/**
 * 工具类
 */
import CryptoJS from 'crypto-js'
import { Base64 } from 'crypto-js';
const keyStr = "k1XWu8pbyPXqZbFU"; //加密密钥
const ivStr = "41cfI4egrhNOjinZ"; //矢量

//加密
export function aes_encrypt(word) {
	let key = CryptoJS.enc.Utf8.parse(keyStr);
	let iv = CryptoJS.enc.Utf8.parse(ivStr);
	const byteArray = CryptoJS.enc.Hex.parse(word);

	let encrypted = CryptoJS.AES.encrypt(byteArray, key, {
		iv: iv,
		mode: CryptoJS.mode.CBC,
		padding: CryptoJS.pad.Pkcs7
	});
	return encrypted.ciphertext.toString().toUpperCase();
}

//解密  
export function aes_decrypt(word) {
	let key = CryptoJS.enc.Utf8.parse(keyStr);
	let iv = CryptoJS.enc.Utf8.parse(ivStr);
	// var restoreBase64 = word.replace(/[\r\n]/g, '');
	// 将加密数据从16进制字符串转换为WordArray类型
	let ciphertext = CryptoJS.enc.Hex.parse(word);

	let decrypt = CryptoJS.AES.decrypt({
		ciphertext: ciphertext
	}, key, {
		iv: iv,
		mode: CryptoJS.mode.CBC,
		padding: CryptoJS.pad.Pkcs7,
	});
	return decrypt.toString(CryptoJS.enc.Hex);
}


// 支付宝手机号解密
export function aes_aliphone(word) {
	let key = CryptoJS.enc.Utf8.parse('/JMFyT4p3kunhPFX+2naAA==');
	let iv = CryptoJS.enc.Utf8.parse(new Uint8Array(16).fill(0));
	let ciphertext = CryptoJS.enc.Base64.parse(word);

	let decrypt = CryptoJS.AES.decrypt({
		ciphertext: ciphertext
	}, key, {
		iv: iv,
		mode: CryptoJS.mode.CBC,
		padding: CryptoJS.pad.Pkcs7,
	});
	console.log(decrypt,"解密的数据")
	return decrypt.toString(CryptoJS.enc.Utf8);
}